package com.b2b.util;

import com.b2b.model.QuotationListDto;
import com.b2b.model.entity.QuotationGoods;
import com.b2b.service.QuotationGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class QuotationGoodsUtil {
    private final QuotationGoodsService quotationGoodsService;

    @Autowired
    public QuotationGoodsUtil(QuotationGoodsService quotationGoodsService){
        this.quotationGoodsService = quotationGoodsService;
    }

    public Integer insertQuotationGoods(QuotationListDto quotationListDto){
        List<QuotationGoods> quotationGoodsList = quotationListDto.getQuotationGoods();
        Integer quotationId = quotationListDto.getQuotationId();
        Integer count = -1;
        for (int i = 0; i < quotationGoodsList.size(); i++) {
            QuotationGoods quotationGoods = quotationGoodsList.get(i);
            quotationGoods.setQuotationId(quotationId);
            count = quotationGoodsService.insert(quotationGoods);
        }
        return count;
    }

    public List<QuotationGoods> selectGoods(Integer quotationId){
        QuotationGoods quotationGoods = new QuotationGoods();
        quotationGoods.setQuotationId(quotationId);

        List<QuotationGoods> quotationGoodsList = quotationGoodsService.select(quotationGoods);

        return quotationGoodsList;
    }

    public List<Integer> getStockId(Integer quotationId){
        List<Integer> stockIds = new ArrayList<>();
        QuotationGoods quotationGoods = new QuotationGoods();
        quotationGoods.setQuotationId(quotationId);

        List<QuotationGoods> quotationGoodsList = quotationGoodsService.select(quotationGoods);

        for (int i = 0; i < quotationGoodsList.size(); i++) {
            Integer stockId = quotationGoodsList.get(i).getStockId();
            stockIds.add(stockId);
        }

        return stockIds;
    }

    public Integer deleteGoods(Integer quotationId){
        List<QuotationGoods> quotationGoodsList = selectGoods(quotationId);
        Integer result = -1;
        for (int i = 0; i < quotationGoodsList.size(); i++) {
            result = quotationGoodsService.delete(quotationGoodsList.get(i));
        }
        return result;
    }

    public Integer deleteGood(QuotationGoods quotationGoods){
        Integer count = quotationGoodsService.deleteByExample(quotationGoods);
        return count;
    }

    public Integer updateGood(QuotationGoods quotationGoods){
        QuotationGoods quotationGoods1 = new QuotationGoods();
        quotationGoods1.setStockId(quotationGoods.getStockId());
        quotationGoods1.setQuotationId(quotationGoods.getQuotationId());
        Integer count = quotationGoodsService.updateByExample(quotationGoods,quotationGoods1);
        return count;
    }
}
